#pragma once

#include <iostream>
#include <string>
#include <cstring>
#include <sys/epoll.h>
#include "log.hpp"
#include "nocopy.hpp"




class Epoller : public nocopy
{
    static const int size = 128;
public:
    Epoller()
    {

        _epfd = epoll_create(size);
        if(_epfd < 0)
        {
            lg(ERROR,"Epoll crate error,errno: %d , errstr:%s",errno,strerror(errno));
        }
        else
        {
            lg(INFO,"Epoll create success");
        }
    }

    int  EpollWait(struct epoll_event revs[],int num)
    {
        int n = epoll_wait(_epfd,revs,num,_timeout);
        return n;
    }


    void EpollUpdate(int oper,int sock,uint32_t event)
    {
        if(oper == EPOLL_CTL_DEL)
        {
            int n = epoll_ctl(_epfd,oper,sock,nullptr);
            if(n != 0)
            {
                lg(ERROR,"epoll_ctl delete error");
            }
        }
        else
        {
            //EPOLL_CTL_ADD EPOLL_CTL_MOD

            struct epoll_event ev;
            ev.data.fd = sock;
            ev.events = event;
            int n = epoll_ctl(_epfd,oper,sock,&ev);
            if(n < 0)
            {
                lg(ERROR,"epoll_ctl ADD or MOD error");
            }
        }
        


        
    }

    ~Epoller()
    {
        if(_epfd > 0)
            close(_epfd);
    }



private:

    int _epfd;
    int _timeout = 3000; 
};

